module('afu.request',package.seeall)

local string_len = string.len
local string_sub = string.sub
local lf = require("afu.luafcgid")
Request = {}

function Request:new(env, con)
    local ret = {
        method          = env.REQUEST_METHOD,
        schema          = env.schema,
        host            = env.HTTP_HOST,
        hostname        = env.SERVER_NAME,
        uri             = env.REQUEST_URI or '',
        path            = lf.pathinfo(env),
        filename        = env.request_filename,
        query_string    = env.QUERY_STRING,
        --headers         = ngx_req.get_headers(),
        user_agent      = env.HTTP_USER_AGENT,
        remote_addr     = env.REMOTE_ADDR,
        remote_port     = env.REMOTE_PORT,
        remote_user     = env.remote_user,
        remote_passwd   = env.remote_passwd,
        content_type    = env.content_type,
        content_length  = env.content_length,
        uri_args        = lf.parse(env.QUERY_STRING),
        --socket          = ngx_req.socket
    }

    setmetatable(ret,self)
    self.__index=self
    return ret
end

function Request:get_uri_arg(name, default)
    if name==nil then return nil end
	if default== nil then default = '' end
    local arg = self.uri_args[name]
    if arg~=nil then
        if type(arg)=='table' then
            for _, v in ipairs(arg) do
                if v and string_len(v)>0 then
                    return v
                end
            end

            return ""
        end

        return arg
    end

    return default
end

function Request:get_post_arg(name, default)
    if name==nil then return nil end
    if self.post_args==nil then return nil end

    local arg = self.post_args[name]
    if arg~=nil then
        --parse post array
        if  string_sub(name, -2) == '[]' and type(arg)=='table' then
            --ngx.log(ngx.INFO, string_sub(name, 1, -3))
            return arg
        end 
        if type(arg)=='table' then
            for _, v in ipairs(arg) do
                if v and string_len(v)>0 then
                    return v
                end
            end

            return ""
        end

        return arg
    end

    return default
end

function Request:get_arg(name, default)
    return self:get_post_arg(name, default) or self:get_uri_arg(name, default)
end

function Request:read_body()
    local ngx_req = ngx.req
    ngx_req.read_body()
    self.post_args = ngx_req.get_post_args()
end

function Request:get_cookie(key, decrypt)
    local value = ngx.var['cookie_'..key]

    if value and value~="" and decrypt==true then
        value = ndk.set_var.set_decode_base64(value)
        value = ndk.set_var.set_decrypt_session(value)
    end

    return value
end

function Request:rewrite(uri, jump)
    return ngx.req.set_uri(uri, jump)
end

function Request:set_uri_args(args)
    return ngx.req.set_uri_args(args)
end

-- to prevent use of casual module global variables
getmetatable(afu.request).__newindex = function (table, key, val)
    error('attempt to write to undeclared variable "' .. key .. '": '
            .. debug.traceback())
end

